<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>计算器</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctxPath}/assets/common/libs/layui/css/layui.css?v=${constants.getReleaseVersion()}"/>
    <link rel="stylesheet" href="${ctxPath}/assets/common/module/admin.css?v=${constants.getReleaseVersion()}" media="all"/>
    <link rel="stylesheet" href="${ctxPath}/assets/expand/css/style.css?v=${constants.getReleaseVersion()}" media="all"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-body-header">
    <span class="layui-body-header-title">计算器</span>
</div>
<div class="layui-form toolbar" style="margin-top: 10px;">
    <div class="layui-form-item">
        <label class="layui-form-label">收费比例(%)</label>
        <div class="layui-inline" style="width: 100px;">
            <input type="number" name="scale" id="scale" placeholder="%" autocomplete="off" class="layui-input" >
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn"  id="addOrderBtn" style="margin-left: 10px;">添加订单</button>
        </div>
    </div>
</div>
<table class="layui-table" id="InsCalculatorTable" lay-filter="InsCalculatorTable"></table>
<div class="btn-circle" id="addPolicy" style="position: absolute; top: 5px;" title="添加保单价格">
    <i class="layui-icon layui-icon-edit"></i>
</div>
<!--订单列表-->
<script type="text/html" id="insOrderZxFqTpl">
    <form id="eDialogCouEditForm" lay-filter="eDialogCouEditForm" class="layui-form">
        <div class="layui-inline" style="margin-left: 15px;margin-top: 10px">
            <div class="layui-input-inline" style="width: 150px;">
                <select  id="channelType" lay-filter="channelTypeSelect">
                    <option value="">请选择渠道类别</option>
                    <option value="1">合伙人</option>
                    <option value="2">保险公司</option>
                    <option value="3">经代渠道</option>
                    <option value="4">个人</option>
                    <option value="5">他业</option>
                    <option value="6">直销</option>
                    <option value="7">无效</option>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 150px;">
                <select class="layui-row" id="channel" lay-filter="channelSelect">
                    <option value="">请先选择渠道类别</option>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 150px;">
                <select class="layui-row" id="insCusromer" lay-filter="insCusromerSelect">
                    <option value="">请先选择渠道</option>
                </select>
            </div>
            <a  class="layui-btn" id="insOrderSearchBtn" style="margin-left: 10px;">查询</a>
        </div>
    </form>
    <table class="layui-table" id="InsOrderZxFqTable" lay-filter="InsOrderZxFqTable" style="margin: 0px;"></table>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="statusBar">
    {{# if(d.status==1){ }}
    <span style="cursor: default;" lay-tips="待审核">待审核</span>
    {{# }else if(d.status==2){ }}
    <span style="cursor: default;" lay-tips="审核通过">审核通过</span>
    {{# } else if(d.status==3){ }}
    <span style="cursor: default;color: red;" lay-tips="审核不通过">审核不通过</span>
    {{# } else if(d.status==4){ }}
    <span style="cursor: default;" lay-tips="同步成功待财务确认">已同步待确认</span>
    {{# } else if(d.status==5){ }}
    <span style="cursor: default;color: red;" lay-tips="同步中信失败">同步中信失败</span>
    {{# } else if(d.status==6){ }}
    <span style="cursor: default;" lay-tips="中信订单审核成功待垫资">审核成功待垫资</span>
    {{# } else if(d.status==7){ }}
    <span style="cursor: default;color: red;" lay-tips="中信订单审核失败">中信订单审核失败</span>
    {{# } else if(d.status==8){ }}
    <span style="cursor: default;" lay-tips="垫资凭证上传成功">垫资凭证上传成功</span>
    {{# } else if(d.status==9){ }}
    <span style="cursor: default;color: red;" lay-tips="垫资凭证上传失败">垫资凭证上传失败</span>
    {{# } else if(d.status==10){ }}
    <span style="cursor: default;" lay-tips="垫资审核成功">垫资审核成功</span>
    {{# } else if(d.status==11){ }}
    <span style="cursor: default;color: red;" lay-tips="垫资审核失败">垫资审核失败</span>
    {{# } else if(d.status==12){ }}
    <span style="cursor: default;" lay-tips="保费已支付">保费已支付</span>
    {{# } else if(d.status==13){ }}
    <span style="cursor: default;" lay-tips="保单资料待审核">保单资料待审核</span>
    {{# } else if(d.status==14){ }}
    <span style="cursor: default;" lay-tips="保单资料审核成功">保单资料审核成功</span>
    {{# } else if(d.status==15){ }}
    <span style="cursor: default;color: red;" lay-tips="保单资料审核失败">保单资料审核失败</span>
    {{# } else if(d.status==16){ }}
    <span style="cursor: default;" lay-tips="保单资料上传成功">保单资料上传成功</span>
    {{# } else if(d.status==17){ }}
    <span style="cursor: default;color: red;" lay-tips="保单资料上传失败">保单资料上传失败</span>
    {{# } else if(d.status==18){ }}
    <span style="cursor: default;" lay-tips="保单资料审核通过已推送银行">保单资料审核通过已推送银行</span>
    {{# } else if(d.status==19){ }}
    <span style="cursor: default;color: red;" lay-tips="保单资料审核失败">保单资料审核失败</span>
    {{# } else if(d.status==20){ }}
    <span style="cursor: default;" lay-tips="实授信成功">实授信成功</span>
    {{# } else if(d.status==21){ }}
    <span style="cursor: default;color: red;" lay-tips="实授信失败">实授信失败</span>
    {{# } else if(d.status==22){ }}
    <span style="cursor: default;" lay-tips="放款成功">放款成功</span>
    {{# } else if(d.status==23){ }}
    <span style="cursor: default;color: red;" lay-tips="放款失败">放款失败</span>
    {{# } else if(d.status==24){ }}
    <span style="cursor: default;" lay-tips="垫资已退还">垫资已退还</span>
    {{# } else if(d.status==25){ }}
    <span style="cursor: default;" lay-tips="订单已结算">订单已结算</span>
    {{# } else if(d.status==88){ }}
    <span style="cursor: default;" lay-tips="已首付待审核">已首付待审核</span>
    {{# } else if(d.status==99){ }}
    <span style="cursor: default;" lay-tips="已垫资待上传">已垫资待上传</span>
    {{# } }}
</script>

<script type="text/javascript">
    var Feng = {
        ctxPath: "${ctxPath}",
        version: '${constants.getReleaseVersion()}'
    };
</script>
<script type="text/javascript" src="${ctxPath}/assets/common/libs/layui/layui.js?v=${constants.getReleaseVersion()}"></script>
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js?v=${constants.getReleaseVersion()}"></script>
<script>

    layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
        var $ = layui.$;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ax = layui.ax;
        var admin = layui.admin;

        // 让当前iframe弹层高度适应
        //admin.iframeAuto();

        /**
         * 客户管理管理初始化
         */
        var InsCalculator = {
            tableId: "InsCalculatorTable",	//表格id
            condition: {}
        };
        //临时数据
        var allData = new Array();

        function loadData(premium,scale,carNo) {
            var loan = parseInt(accMul(premium, 0.9));
            var first = accSubtr(premium, loan);
            var serviceFee = roundup(accMul(premium,0.02),2);
            var zx = accAdd(first, serviceFee);
            var scale = accAdd(scale * 100, 100);
            var aa = accMul(accMul(premium, scale), 0.01);
            var result = parseFloat(accSubtr(aa, loan)).toFixed(2);

            var obj = {
                premium: premium,
                loan: loan,
                first:first,
                serviceFee:serviceFee,
                zx:zx,
                lb:result,
                carNo: carNo
            }
            allData.push(obj);
        }

        /**
         * 初始化表格的列
         */
        InsCalculator.initColumn = function () {
            return [[
                {type: 'numbers', title: '序号', totalRowText: '合计'},
                {field: 'carNo', sort: false, title: '车牌'},
                {field: 'premium', sort: false, title: '保费', totalRow: true},
                {field: 'loan', sort: false, title: '贷款', totalRow: true},
                {field: 'first', sort: false, title: '首付款', totalRow: true},
                {field: 'serviceFee', sort: false, title: '服务费', totalRow: true},
                {field: 'zx', sort: false, title: '给中信', totalRow: true},
                {field: 'lb', sort: false, title: '给履保', totalRow: true},
                {align: 'center', title: '操作',templet: function (data) {
                        return '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    }}
            ]]
        };


        /**
         * 查询中信分期订单列表
         */
        InsCalculator.search = function () {
            var queryData = {};
            queryData['customerCode'] = $("#insCusromer").val();
            table.reload('InsOrderZxFqTable', {where: queryData, page: {curr: 1 }});
        };

        //添加已有订单
        $('#addOrderBtn').click(function () {

            openOrderDialog();
        });

        $("#scale").bind("input propertychange",function(event){
            var tempData = allData;
            allData = [];
            var scale = this.value;
            $.each(tempData, function (i, item) {
                loadData(item.premium, accMul(scale, 0.01), item.carNo);
            });
            table.reload('InsCalculatorTable', {data: allData});
        });

        function openOrderDialog() {
            layer.open({
                type: 1,
                title: '中信订单',
                anim: 7,
                scrollbar: false,
                area: ['650px', '450px'],
                btn: ['确定', '关闭'],
                content: $("#insOrderZxFqTpl").text(),
                success: function (layero, index) {
                    table.render({
                        elem: '#InsOrderZxFqTable',
                        url: Feng.ctxPath + '/insOrderFqZx/list?statusArr=1,2,3,4,5,88,6,7,99,8,9,10,12',
                        title: '中信订单',
                        height: 'full-150',
                        even: true,
                        page: true,
                        cols: [[
                            {type: 'checkbox'},
                            {title: '客户名称', field: 'customerName', visible: true, align: 'center', valign: 'middle'},
                            {title: '车牌', field: 'carNo', visible: true, align: 'center', valign: 'middle', width: 120},
                            {title: '保费', field: 'premium', visible: true, align: 'center', valign: 'middle'},
                            {title: '比例', field: 'customerScale', visible: true, align: 'center', valign: 'middle'},
                            {title: '订单状态', field: 'status', visible: true, align: 'center', valign: 'middle',toolbar: '#statusBar'},
                        ]],
                    });
                    form.render('select');
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                },yes: function(index, layero){
                    var checkStatus = table.checkStatus('InsOrderZxFqTable');
                    if(checkStatus.data.length == 0){
                        Feng.error("您当前没有选择任何订单!");
                        return;
                    }
                    var scale = 0;
                    var success = true;
                    var carNoAll = '';
                    var scaleValue = $("#scale").val();
                    $.each(checkStatus.data, function (i, item) {
                        if(success){
                            var customerScale  = item.customerScale == '' ? 0 : item.customerScale;
                            if(scaleValue == ''){
                                if(scale == 0){
                                    scale = customerScale;
                                }
                                if(scale != customerScale){
                                    success = false;
                                    carNoAll = item.carNo;
                                    return false;
                                }
                            } else {
                                if(scaleValue != accMul(customerScale, 100)){
                                    success = false;
                                    carNoAll = item.carNo;
                                    return false;
                                }
                            }
                        }
                    });
                    if(!success){
                        Feng.info(carNoAll + " - 与当前订单比例不一致");
                        return;
                    }

                    if(scaleValue == ''){
                        $("#scale").val(accMul(scale, 100));
                    }
                    $.each(checkStatus.data, function (i, item) {
                        var customerScale  = item.customerScale == '' ? 0 : item.customerScale;
                        var premium  = item.premium == '' ? 0 : item.premium;
                        var carNo  = item.carNo == '' ? '' : item.carNo;
                        loadData(premium,customerScale,carNo);

                    });
                    table.reload('InsCalculatorTable', {data: allData});
                    Feng.info("添加成功");
                    layer.close(index);
                },btn2: function(index, layero){
                    //取消不做任何处理
                }
            });
            //渠道类型
            form.on('select(channelTypeSelect)', function (selectData) {

                if(selectData.value == ''){
                    $("#channel").empty().append('<option value="">请选择渠道</option>');
                    form.render();
                } else {
                    var ajax = new $ax(Feng.ctxPath + "/insChannel/list", function (data) {
                        var html = '<option value="">请选择渠道</option>';
                        if(data.data.length > 0){
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.channelCode + '">' + item.channelName + '</option>';
                            });
                            $("#channel").empty().append(html);
                            form.render();
                        }
                    }, function (data) {
                        Feng.error("加载失败!" + data.responseJSON.message + "!");
                    }, true);
                    ajax.set("page", 1);
                    ajax.set("limit", 1000);
                    ajax.set("channelType", selectData.value);
                    ajax.set("channelLevel", 1);
                    ajax.start();
                }
            });

            //渠道类型
            form.on('select(channelSelect)', function (selectData) {

                if(selectData.value == ''){
                    $("#channel").empty().append('<option value="">请选择客户</option>');
                    form.render();
                } else {
                    var ajax = new $ax(Feng.ctxPath + "/insCustomer/list", function (data) {
                        var html = '<option value="">请选择客户</option>';
                        if(data.data.length > 0){
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.customerCode + '">' + item.customerName + '</option>';
                            });
                            $("#insCusromer").empty().append(html);
                            form.render();
                        }
                    }, function (data) {
                        Feng.error("加载失败!" + data.responseJSON.message + "!");
                    }, true);
                    ajax.set("page", 1);
                    ajax.set("limit", 1000);
                    ajax.set("channelCode", selectData.value);
                    ajax.start();
                }
            });

            // 搜索按钮点击事件
            $('#insOrderSearchBtn').click(function () {
                InsCalculator.search();
            });
        }

        var promptIndex;
        $('#addPolicy').click(function () {
            var scale = $("#scale").val();
            if (scale == '' || isNaN(scale)) {
                Feng.error("请先输入收费比例");
                return;
            }
            promptIndex = layer.prompt({
                title: '添加保单价格',
                shade: .1,
                offset: '165px',
                skin: 'layui-layer-admin layui-layer-prompt',
                formType: 0
            }, function (value, index, elem) {
                if (isNaN(value)) {
                    Feng.errorMsg("请输入数值类型,请检查");
                    return;
                }
                loadData(value, accMul(scale, 0.01), '');
                table.reload('InsCalculatorTable', {data: allData});
                Feng.info("添加成功");
                layer.close(index);
            });
        });


        // 渲染表格
        var tableResult = table.render({
            elem: '#' + InsCalculator.tableId,
            totalRow: true,
            height: 'full-60',
            limit: 1000,
            cols: InsCalculator.initColumn(),
            data: []
        });

        // 查看评论工具条点击事件
        table.on('tool(InsCalculatorTable)', function (obj) {
            var index = obj.tr.data('index');
            allData.splice(index, 1);
            table.reload('InsCalculatorTable', {data: allData});
        });

        $(document).keypress(function (e) {
            var value = $(".layui-layer-prompt .layui-layer-content .layui-layer-input").val();
            // 回车键事件
            if (e.which == 13) {
                if(value == undefined){
                    return;
                }
                if(value == ''){
                    Feng.info("请填写保费");
                    return;
                }
                if (isNaN(value)) {
                    Feng.errorMsg("请输入数值类型,请检查");
                    return;
                }
                var scale = $("#scale").val();
                loadData(value, accMul(scale, 0.01), '');
                table.reload('InsCalculatorTable', {data: allData});
                Feng.info("添加成功");
                layer.close(promptIndex);
            }
        });

        /**
         * roundup计算进位
         */
        function roundup(num, dep) {
            var  dep = Math.floor(dep);
            return (dep < 0 ? num : Math.ceil(num * Math.pow(10, dep)) / (Math.pow(10, dep)));
        }


        /**
         * 加法
         */
        function accAdd(arg1, arg2) {
            var r1, r2, m;
            try {
                r1 = arg1.toString().split(".")[1].length
            } catch (e) {
                r1 = 0
            }
            ;
            try {
                r2 = arg2.toString().split(".")[1].length
            } catch (e) {
                r2 = 0
            }
            ;
            m = Math.pow(10, Math.max(r1, r2));
            return accDivCoupon((arg1 * m + arg2 * m),m);
        }

        /**
         * 减法
         */
        function accSubtr(arg1, arg2) {
            var r1, r2, m, n;
            try {
                r1 = arg1.toString().split(".")[1].length;
            } catch (e) {
                r1 = 0;
            }
            try {
                r2 = arg2.toString().split(".")[1].length;
            } catch (e) {
                r2 = 0;
            }
            m = Math.pow(10, Math.max(r1, r2));
            //动态控制精度长度
            n = (r1 >= r2) ? r1 : r2;
            return ((arg1 * m - arg2 * m) / m).toFixed(n);
        }

        /***
         * 乘法，获取精确乘法的结果值
         */
        function accMul(arg1, arg2) {
            var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
            try {
                m += s1.split(".")[1].length
            } catch (e) {
            }
            ;
            try {
                m += s2.split(".")[1].length
            } catch (e) {
            }
            ;
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
        }

        /***
         * 除法，获取精确乘法的结果值
         */
        function accDivCoupon(arg1, arg2) {
            var t1 = 0, t2 = 0, r1, r2;
            try {
                t1 = arg1.toString().split(".")[1].length;
            } catch (e) {
            }
            try {
                t2 = arg2.toString().split(".")[1].length;
            } catch (e) {
            }
            with (Math) {
                r1 = Number(arg1.toString().replace(".", ""));
                r2 = Number(arg2.toString().replace(".", ""));
                return (r1 / r2) * pow(10, t2 - t1);
            }
        }

    });


</script>
</body>
</html>